//
//  ElarcSwitchTestController.swift
//  elarc01
//
//  Created by elarc on 16/8/16.
//  Copyright © 2016年 elarc. All rights reserved.
//

import Foundation
class ElarcSwitchTestController: UIViewController {
        
        private var connectContactsLabel: UILabel!
        private var phone1ImageView: UIImageView!
        private var paperSwitch1: ElarcSwitchControl!
        
        private var allowDiscoveryLabel: UILabel!
        private var phone2ImageView: UIImageView!
        private var paperSwitch2: ElarcSwitchControl!
        
        
        override func viewDidLoad() {
            super.viewDidLoad()
            view.backgroundColor = UIColor.whiteColor()
            configSubViews()
        }
        
        private func configSubViews() {
            
            connectContactsLabel = UILabel()
            connectContactsLabel.frame = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height/2)
            
            
            phone1ImageView = UIImageView()
            phone1ImageView.frame = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height/2)
            
            paperSwitch1 = ElarcSwitchControl(view: self.view,color: UIColor.redColor())
            paperSwitch1.frame = CGRectMake(100, 120, 100, 44)
            
            
            allowDiscoveryLabel = UILabel()
            allowDiscoveryLabel.frame = CGRectMake(0,  UIScreen.mainScreen().bounds.height/2, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height/2)
            
            phone2ImageView = UIImageView()
            phone2ImageView.frame = CGRectMake(0,  UIScreen.mainScreen().bounds.height/2, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height/2)
            
            paperSwitch2 = ElarcSwitchControl(view: self.view,color: UIColor.yellowColor())
            
            paperSwitch2.frame = CGRectMake(100, 300, 100, 44)
            
            self.view.addSubview(phone1ImageView)
            self.view.addSubview(connectContactsLabel)
            self.view.addSubview(paperSwitch1)
   
            self.view.addSubview(phone2ImageView)
            self.view.addSubview(allowDiscoveryLabel)
            self.view.addSubview(paperSwitch2)
            
            connectContactsLabel.text = "好吃好喝"
            allowDiscoveryLabel.text = "人生快活"
            
            setupPaperSwitch()
            
            
        }
        
        private func setupPaperSwitch() {
            
            self.paperSwitch1.animationDidStartClosure = {(onAnimation: Bool) in
                
                self.animateLabel(self.connectContactsLabel, onAnimation: onAnimation, duration: self.paperSwitch1.duration)
                self.animateImageView(self.phone1ImageView, onAnimation: onAnimation, duration: self.paperSwitch1.duration)
            }
            
            
            self.paperSwitch2.animationDidStartClosure = {(onAnimation: Bool) in
                
                self.animateLabel(self.self.allowDiscoveryLabel, onAnimation: onAnimation, duration: self.paperSwitch2.duration)
                self.animateImageView(self.phone2ImageView, onAnimation: onAnimation, duration: self.paperSwitch2.duration)
            }
        }
        
        
        private func animateLabel(label: UILabel, onAnimation: Bool, duration: NSTimeInterval) {
            UIView.transitionWithView(label, duration: duration, options: UIViewAnimationOptions.TransitionFlipFromBottom, animations: {
                label.textColor = onAnimation ? UIColor.whiteColor() : UIColor(red: 31/255.0, green: 183/255.0, blue: 252/255.0, alpha: 1)
                }, completion:nil)
        }
        
        
        private func animateImageView(imageView: UIImageView, onAnimation: Bool, duration: NSTimeInterval) {
            UIView.transitionWithView(imageView, duration: duration, options: UIViewAnimationOptions.TransitionFlipFromLeft, animations: {
                imageView.image = UIImage(named: onAnimation ? "01" : "02")
                }, completion:nil)
        }
        
}

